// 2.两数相加
package LeetcodeHot100;


public class Solution002 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode result = pre;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int value1 = l1 == null ? 0 : l1.val;
            int value2 = l2 == null ? 0 : l2.val;
            carry = carry + value1 + value2;
            ListNode node = new ListNode(carry % 10);
            pre.next = node;
            pre = pre.next;
            carry /= 10;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        if (carry != 0) {
            ListNode node = new ListNode(carry);
            pre.next = node;
        }
        return result.next;
    }
}
